package ru.autofan.secure.aop;

import java.lang.reflect.Method;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;

import ru.autofan.secure.Restricted;

public class RestrictecdPointcut implements Pointcut {

	public RestrictecdPointcut() {
	}

	private ClassFilter classFilter;

	public ClassFilter getClassFilter() {
		if (classFilter == null) {
			classFilter = new ClassFilter() {

				public boolean matches(Class clazz) {
					return true;
				}
				
			}; 
		}
		return classFilter;
	}

	private MethodMatcher methodMatcher;

	class RestrictedMatcher implements MethodMatcher {

		public boolean isRuntime() {
			return false;
		}

		public boolean matches(Method method, Class targetClass) {
			return matches(method, targetClass, null);
		}

		public boolean matches(Method method, Class targetClass, Object[] args) {
			if (method.getAnnotation(Restricted.class) == null) {
				return false;
			}
			return true;
		}

	}

	public MethodMatcher getMethodMatcher() {
		if (methodMatcher == null) {
			methodMatcher = new RestrictedMatcher();
		}
		return methodMatcher;
	}

}
